package com.unknown.wiki;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.unknown.common.util.ActivityUtils;
import com.unknown.common.widget.CommonRecyclerViewAdapter;
import com.unknown.common.widget.CommonViewHolder;
import com.unknown.communication.entity.Message;
import com.unknown.wiki.base.BaseActivity;
import com.unknown.wiki.details.view.WikiDetailsActivity;
import com.unknown.wiki.model.Wiki;

import java.util.ArrayList;
import java.util.List;

public class WikiListActivity extends BaseActivity {
    private CommonRecyclerViewAdapter<Wiki> commonRecyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wiki_list);

        initWikiList();
        getWikiList();
    }

    private void initWikiList() {
        RecyclerView rvWikiList = findViewById(R.id.main_wiki_list_rv);
        commonRecyclerViewAdapter = new CommonRecyclerViewAdapter<Wiki>(this, R.layout.item_wiki_list) {
            @Override
            public void bind(CommonViewHolder commonViewHolder, Wiki item, List<Object> payloads) {
                commonViewHolder.setText(R.id.main_name_tv, item.getName())
                        .setOnclickListener(R.id.main_item_cl, v -> showWikiDetails());
            }
        };
        commonRecyclerViewAdapter.bindRecyclerView(rvWikiList);
    }

    private void getWikiList() {
        List<Wiki> tests = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Wiki wiki = new Wiki();
            wiki.setName("wiki name");
            tests.add(wiki);
        }
        commonRecyclerViewAdapter.setData(tests);
    }

    private void showWikiDetails() {
        ActivityUtils.startActivity(this, WikiDetailsActivity.class, false);
        sendMessage(new Message(WikiListActivity.class, WikiDetailsActivity.class));
    }
}
